﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Caliburn.Micro.BikeWizard.Utils
{
    /// <summary>
    /// Executes an action after a specified time
    /// </summary>
    public class TimeoutAction
    {
        private DispatcherTimer timer = new DispatcherTimer();

        // Default time before action is executed is 10 seconds
        public static Func<TimeSpan> DefaultTimeoutBeforeActionFires = () => new TimeSpan(0, 0, 10);

        public void Cancel()
        {
            timer.Stop();
        }

        public TimeoutAction(System.Action action) : this(action, DefaultTimeoutBeforeActionFires())
        {
        }

        public TimeoutAction(System.Action action, TimeSpan timeoutBeforeActionFires)
        {
            timer = new DispatcherTimer();
            timer.Interval = timeoutBeforeActionFires;
            timer.Tick +=
                (sender, e) =>
                {
                    timer.Stop();
                    action();
                };
            timer.Start();
        }
    }
}
